package com.cxm.qyyz.utils;

import android.app.Activity;
import android.content.ContentResolver;
import android.content.Context;
import android.content.res.Resources;
import android.net.Uri;
import android.text.TextUtils;
import android.util.TypedValue;
import android.widget.ImageView;

import androidx.annotation.DrawableRes;
import androidx.fragment.app.Fragment;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;
import com.bumptech.glide.request.target.BitmapImageViewTarget;
import com.cxm.qyyz.R;
import com.cxm.qyyz.app.App;
import com.cxm.qyyz.app.Constants;

public class ImageLoader {

    public static void load(Activity fragment, ImageView ivTarget, String url) {
        if (!fragment.isFinishing()) {
            Glide.with(fragment)
                    .load(url)
                    .skipMemoryCache(true)
                    .diskCacheStrategy(DiskCacheStrategy.DATA)
                    .into(ivTarget);
        }
    }

    public static void load(Context context, ImageView ivTarget, String url,int widthDp,int heightDp) {
        int width = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, widthDp, context.getResources().getDisplayMetrics());
        int height = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, heightDp, context.getResources().getDisplayMetrics());
        Glide.with(context)
                .load(url)
                .override(width,height)
                .dontTransform()
                .encodeQuality(80)
                .into(ivTarget);
    }

    public static void loadBySize(Context context, ImageView ivTarget, String url,int sizeDp) {
        int size = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, sizeDp, context.getResources().getDisplayMetrics());
        Glide.with(context)
                .load(url)
                .dontTransform()
                .encodeQuality(80)
                .override(size)
                .into(ivTarget);
    }

    public static void loadNoCache(Activity fragment, ImageView ivTarget, String url) {
        Glide.with(fragment)
                .load(url)
                .into(ivTarget);
    }

    public static void load(Context fragment, ImageView ivTarget, String url) {
        if (null != fragment)
            Glide.with(fragment)
                    .load(url)
                    .skipMemoryCache(true)
                    .diskCacheStrategy(DiskCacheStrategy.DATA)
                    .into(ivTarget);
    }

    public static void loadOriginalBmp(Context fragment, ImageView ivTarget, String url) {
        if (null != fragment)
            Glide.with(fragment)
                    .asBitmap()
                    .load(url)
                    .skipMemoryCache(true)
                    .diskCacheStrategy(DiskCacheStrategy.DATA)
                    .into(new BitmapImageViewTarget(ivTarget));
    }

    public static void loadByIv(Context fragment, ImageView ivTarget, String url) {
        if (null != fragment) {
            ivTarget.post(() -> Glide.with(fragment)
                    .load(url)
                    .override(ivTarget.getWidth())
                    .dontAnimate()
                    .skipMemoryCache(false)
                    .diskCacheStrategy(DiskCacheStrategy.DATA)
                    .into(ivTarget));
        }
    }

    public static void load2(Context fragment, ImageView ivTarget, String url) {
//        if (null != fragment)
//            Glide.with(fragment)
//                    .load(url)
//                    .skipMemoryCache(true)
//                    .diskCacheStrategy(DiskCacheStrategy.DATA)
//                    .error(R.drawable.ic_logo)
//                    .placeholder(R.drawable.ic_logo)
//                    .into(ivTarget);
    }

    public static void loadNoCache(Context fragment, ImageView ivTarget, String url) {
        if (null != fragment)
            Glide.with(fragment)
                    .load(url)
                    .into(ivTarget);
    }

    public static void loadNoCache(Context fragment, ImageView ivTarget, int url) {
        if (null != fragment)
            Glide.with(fragment)
                    .load(url)
                    .into(ivTarget);
    }

    public static void loadWithFragment(Fragment fragment, ImageView ivTarget, String url, int size) {
        loadWithFragment(fragment, ivTarget, url, size, size);
    }

    public static void loadWithFragment(Fragment fragment, ImageView ivTarget, String url, int width, int height) {
        if (TextUtils.isEmpty(url) || ivTarget == null) {
            return;
        }
        if (url.startsWith("https://mdhw.oss-cn-hangzhou.aliyuncs.com/")) {
            Glide.with(fragment)
                    .load(url + "?x-oss-process=image/resize,m_lfit,h_" + height + ",w_" + width)
                    .skipMemoryCache(true)
                    .diskCacheStrategy(DiskCacheStrategy.DATA)
                    .override(width, height)
                    .into(ivTarget);
        } else {
            Glide.with(fragment)
                    .load(url)
                    .skipMemoryCache(true)
                    .diskCacheStrategy(DiskCacheStrategy.DATA)
                    .override(width, height)
                    .into(ivTarget);
        }
    }

    public static void loadWithActivity(Activity activity, ImageView ivTarget, String url, int size) {
        loadWithActivity(activity, ivTarget, url, size, size);
    }

    public static void loadWithActivity(Activity activity, ImageView ivTarget, String url, int width, int height) {
        if (TextUtils.isEmpty(url) || ivTarget == null) {
            return;
        }
        if (url.startsWith("https://mdhw.oss-cn-hangzhou.aliyuncs.com/")) {
            Glide.with(activity)
                    .load(url + "?x-oss-process=image/resize,m_lfit,h_" + height + ",w_" + width)
                    .skipMemoryCache(true)
                    .diskCacheStrategy(DiskCacheStrategy.DATA)
                    .override(width, height)
                    .into(ivTarget);
        } else {
            Glide.with(activity)
                    .load(url)
                    .skipMemoryCache(true)
                    .diskCacheStrategy(DiskCacheStrategy.DATA)
                    .override(width, height)
                    .into(ivTarget);
        }
    }

    public static void loadWithContext(Context context, ImageView ivTarget, String url) {
    }


    public static void loadWithContext(Context context, ImageView ivTarget, String url, int size) {
        loadWithContext(context, ivTarget, url, size, size);
    }

    public static void loadWithContext(Context context, ImageView ivTarget, String url, int size, boolean cacheEnabled) {
        loadWithContext(context, ivTarget, url, size, size, cacheEnabled);
    }

    public static void loadWithContext(Context context, ImageView ivTarget, String url, int width, int height) {
        loadWithContext(context, ivTarget, url, width, height, true);
    }

    public static void loadWithContext(Context context, ImageView ivTarget, String url, int width, int height, boolean cacheEnabled, int placeHolder) {
        if (TextUtils.isEmpty(url) || ivTarget == null) {
            return;
        }
        if (url.startsWith("https://mdhw.oss-cn-hangzhou.aliyuncs.com/")) {
            Glide.with(context)
                    .load(url + "?x-oss-process=image/resize,m_lfit,h_" + height + ",w_" + width)
                    .skipMemoryCache(!cacheEnabled)
                    .diskCacheStrategy(DiskCacheStrategy.DATA)
                    .override(width, height)
                    .into(ivTarget);
        } else {
            if (placeHolder != 0) {
                Glide.with(context)
                        .load(url)
                        .skipMemoryCache(!cacheEnabled)
                        .placeholder(placeHolder)
                        .diskCacheStrategy(DiskCacheStrategy.DATA)
                        .override(width, height)
                        .into(ivTarget);
            } else {
                Glide.with(context)
                        .load(url)
                        .skipMemoryCache(!cacheEnabled)
                        .diskCacheStrategy(DiskCacheStrategy.DATA)
                        .override(width, height)
                        .into(ivTarget);
            }
        }
    }

    public static void loadWithContext(Context context, ImageView ivTarget, String url, int width, int height, boolean cacheEnabled) {
        loadWithContext(context, ivTarget, url, width, height, cacheEnabled, 0);
    }

    public static void loadWithContext(Context context, ImageView ivTarget, String url, @DrawableRes int placeRes, int width, int height, boolean cacheEnabled) {
        if (TextUtils.isEmpty(url) || ivTarget == null) {
            return;
        }
        if (url.startsWith("https://mdhw.oss-cn-hangzhou.aliyuncs.com/")) {
            Glide.with(context)
                    .load(url + "?x-oss-process=image/resize,m_lfit,h_" + height + ",w_" + width)
                    .skipMemoryCache(!cacheEnabled)
                    .error(placeRes)
                    .placeholder(placeRes)
                    .diskCacheStrategy(DiskCacheStrategy.DATA)
                    .override(width, height)
                    .into(ivTarget);
        } else {
            Glide.with(context)
                    .load(url)
                    .skipMemoryCache(!cacheEnabled)
                    .error(placeRes)
                    .placeholder(placeRes)
                    .diskCacheStrategy(DiskCacheStrategy.DATA)
                    .override(width, height)
                    .into(ivTarget);
        }
    }

    public static void loadWithContext(Context context, ImageView ivTarget, @DrawableRes int resId, int size) {
        Glide.with(context)
                .load(resId)
                .skipMemoryCache(true)
                .diskCacheStrategy(DiskCacheStrategy.DATA)
                .override(size, size)
                .into(ivTarget);
    }

    public static void loadWithCrop(Context context, ImageView ivTarget, String url, int size, boolean cacheEnabled) {
        if (TextUtils.isEmpty(url) || ivTarget == null) {
            return;
        }
        if (url.startsWith("https://mdhw.oss-cn-hangzhou.aliyuncs.com/")) {
            Glide.with(context)
                    .load(url + "?x-oss-process=image/resize,m_lfit,h_" + size + ",w_" + size)
                    .skipMemoryCache(!cacheEnabled)
                    .diskCacheStrategy(DiskCacheStrategy.DATA)
                    .override(size, size)
                    .centerCrop()
                    .into(ivTarget);
        } else {
            Glide.with(context)
                    .load(url)
                    .skipMemoryCache(!cacheEnabled)
                    .diskCacheStrategy(DiskCacheStrategy.DATA)
                    .override(size, size)
                    .centerCrop()
                    .into(ivTarget);
        }
    }

    public static void loadWithContextByDefault(Context context, ImageView ivTarget, String url, int size, @DrawableRes int placeRes) {
        loadWithContextByDefault(context, ivTarget, url, size, size, placeRes);
    }

    public static void loadWithContextByDefault(Context context, ImageView ivTarget, String url, int width, int height, @DrawableRes int placeRes) {
        if (TextUtils.isEmpty(url) || ivTarget == null) {
            return;
        }
        if (url.startsWith("https://mdhw.oss-cn-hangzhou.aliyuncs.com/")) {
            Glide.with(context)
                    .load(url + "?x-oss-process=image/resize,m_lfit,h_" + height + ",w_" + width)
                    .skipMemoryCache(true)
                    .diskCacheStrategy(DiskCacheStrategy.DATA)
                    .placeholder(placeRes)
                    .error(placeRes)
                    .override(width, height)
                    .into(ivTarget);
        } else {
            Glide.with(context)
                    .load(url)
                    .skipMemoryCache(true)
                    .diskCacheStrategy(DiskCacheStrategy.DATA)
                    .placeholder(placeRes)
                    .error(placeRes)
                    .into(ivTarget);
        }
    }


    /**
     * @param activity    当前页面对象
     * @param ivTarget    需要设置图片的view
     * @param url         图片地址
     * @param placeholder 占位图
     */
    public static void loadCrossFade(Activity activity, ImageView ivTarget, String url, int placeholder, int w, int h) {

        if (ivTarget == null) {
            return;
        }
        if (!TextUtils.isEmpty(url) && url.startsWith("https://mdhw.oss-cn-hangzhou.aliyuncs.com/")) {
            Glide.with(activity)
                    .load(url + "?x-oss-process=image/resize,m_lfit,h_" + h + ",w_" + w)
                    .skipMemoryCache(true)
                    .diskCacheStrategy(DiskCacheStrategy.DATA)
                    .placeholder(placeholder)
                    .error(placeholder)
                    .override(w, h)
                    .into(ivTarget);
        } else {
            Glide.with(activity)
                    .load(url)
                    .skipMemoryCache(true)
                    .diskCacheStrategy(DiskCacheStrategy.DATA)
                    .placeholder(placeholder)
                    .error(placeholder)
                    .into(ivTarget);
        }
    }


    public static void loadCrossFade(Context activity, ImageView ivTarget, String url, int placeholder, int w, int h) {

        if (ivTarget == null) {
            return;
        }
        if (!TextUtils.isEmpty(url) && url.startsWith(Constants.IMAGE_URL)) {
            Glide.with(activity)
                    .load(url + "?x-oss-process=image/resize,m_lfit,h_" + h + ",w_" + w)
                    .skipMemoryCache(true)
                    .placeholder(placeholder)
                    .error(placeholder)
                    .override(w, h)
                    .into(ivTarget);
        } else {
            Glide.with(activity)
                    .load(url)
                    .skipMemoryCache(true)
                    .diskCacheStrategy(DiskCacheStrategy.DATA)
                    .placeholder(placeholder)
                    .error(placeholder)
                    .into(ivTarget);
        }
    }

    /**
     * @param activity       当前页面对象
     * @param ivTarget       需要设置图片的view
     * @param url            图片地址
     * @param placeholder    占位图
     * @param error          失败后的图片
     * @param roundedCorners 圆角
     */
    public static void loadRoundedCorners(Context activity, ImageView ivTarget, String url, int placeholder, int error, int roundedCorners, int w, int h) {

        if (TextUtils.isEmpty(url) || ivTarget == null) {
            return;
        }
        if (url.startsWith("https://mdhw.oss-cn-hangzhou.aliyuncs.com/")) {
            Glide.with(activity)
                    .load(url + "?x-oss-process=image/resize,m_lfit,h_" + h + ",w_" + w)
                    .skipMemoryCache(false)
                    .diskCacheStrategy(DiskCacheStrategy.DATA)
                    .placeholder(placeholder)
                    .error(error)
                    .override(w, h)
                    .apply(RequestOptions.bitmapTransform(new RoundedCorners(roundedCorners)))
                    .into(ivTarget);
        } else {
            Glide.with(activity)
                    .load(url)
                    .skipMemoryCache(false)
                    .diskCacheStrategy(DiskCacheStrategy.DATA)
                    .placeholder(placeholder)
                    .error(error)
                    .apply(RequestOptions.bitmapTransform(new RoundedCorners(roundedCorners)))
                    .into(ivTarget);
        }
    }

    /**
     * DrawableToUir
     * date 2021/12/22
     * author yzx
     *
     * @param ids 图片id
     */
    public static String getDrawableToUir(int ids) {
        Resources resources = App.getInstance().getResources();
        Uri parse = Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE + "://"
                + resources.getResourcePackageName(ids) + "/"
                + resources.getResourceTypeName(ids) + "/"
                + resources.getResourceEntryName(ids));
        return parse.toString();
    }
}
